//
//  ZTXYiChangChuLiImageCell.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2023/12/7.
//  Copyright © 2023 GCY. All rights reserved.
//

#import "ZTXYiChangChuLiImageCell.h"
#import "TZImagePickerController.h"
#import "ZXCollectionCell.h"
#import "STPickerView.h"
@interface ZTXYiChangChuLiImageCell ()<TZImagePickerControllerDelegate,ZXCollectionCellDelegate, UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *imageUrlArray;
@end

@implementation ZTXYiChangChuLiImageCell
- (void)fuzhiImagArr:(NSString *)imgStr{
    NSArray *arr = [imgStr componentsSeparatedByString:@","];
    self.imageUrlArray = [NSMutableArray array];
    for (NSString *imgStr in arr) {
        [self.imageUrlArray addObject:imgStr];
    }
    if(arr.count > 8){
        self.collectionView.frame = CGRectMake(0, 0, ScreenW, (ScreenW-20)/4 * 3);
    }else if (arr.count > 4 && arr.count < 8){
        self.collectionView.frame = CGRectMake(0, 0, ScreenW, (ScreenW-20)/4 * 2);
    }else{
        self.collectionView.frame = CGRectMake(0, 0, ScreenW, (ScreenW-20)/4);
    }
    [self.collectionView reloadData];
}
+ (CGFloat)cententImagHeight:(NSString *)imgStr{
    NSArray *arr = [imgStr componentsSeparatedByString:@","];
    if(arr.count > 8){
        return (ScreenW-20)/4 * 3 + 30;
    }else if (arr.count > 4 && arr.count < 8){
        return (ScreenW-20)/4 * 2 + 30;
    }else{
        return (ScreenW-20)/4 + 30;
    }
   
}
- (void)awakeFromNib {
    [super awakeFromNib];
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 100) collectionViewLayout:flowLayout];
    self.collectionView.scrollEnabled = NO;
    //设置代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [_colloreBgView addSubview:self.collectionView];
    self.collectionView.backgroundColor = BackgroundColor;
    //注册cell和ReusableView（相当于头部）
    [self.collectionView registerClass:[ZXCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageUrlArray.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    ZXCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
//    [cell sizeToFit];
//    if (!cell) {
//        NSLog(@"无法创建CollectionViewCell时打印，自定义的cell就不可能进来了。");
//    }
//    if (indexPath.item == 0) {
        cell.close.hidden = YES;
//    } else {
//       cell.close.hidden = NO;
//    }
//    cell.imgView.image = [self.imageUrlArray objectAtIndex:indexPath.row];
    [cell.imgView sd_setImageWithURL:self.imageUrlArray[indexPath.row]];
    cell.delegate = self;
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //边距占5*4=20 ，2个
    //图片为正方形，边长：(fDeviceWidth-20)/2-5-5 所以总高(fDeviceWidth-20)/2-5-5 +20+30+5+5 label高20 btn高30 边
    return CGSizeMake((ScreenW-20)/4, (ScreenW-20)/4);
}
//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 5, 5, 5);
}
//定义每个UICollectionView 纵向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    
    NSLog(@"选择%ld",indexPath.row);
        
    //查看
    HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
    browser.isFullWidthForLandScape = YES;
    browser.isNeedLandscape = YES;
    browser.currentImageIndex = (int)(indexPath.item);
    browser.imageArray = weakSelf.imageUrlArray;
    [browser show];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)moveImageBtnClick:(ZXCollectionCell *)aCell{
    NSIndexPath * indexPath = [self.collectionView indexPathForCell:aCell];
    NSLog(@"删除了第%ld个图片",indexPath.row);
    [self.imageUrlArray removeObjectAtIndex:indexPath.row-1];
    [self.collectionView reloadData];
}
@end
